package com.osscode.framework.core.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DES {
	private static byte[] iv = { '1', '2', '3', '4', '5', '6', '7', '8' };
	private static String keyStr = "12345678";
	private final static String HEX = "0123456789ABCDEF";

	
	private static String encryptDES(String encryptString) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(iv);

		SecretKeySpec key = new SecretKeySpec(keyStr.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

		return toHex(encryptedData);
	}

	private static String decryptDES(String decryptString) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(iv);

		SecretKeySpec key = new SecretKeySpec(keyStr.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
		byte decryptedData[] = cipher.doFinal(toByte(decryptString));

		return new String(decryptedData);
	}

	private static byte[] toByte(String hexString) {
		int len = hexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++)
			result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
					16).byteValue();
		return result;
	}

	private static String toHex(byte[] buf) {
		if (buf == null)
			return "";
		StringBuffer result = new StringBuffer(2 * buf.length);
		for (int i = 0; i < buf.length; i++)
			appendHex(result, buf[i]);

		return result.toString();
	}

	private static void appendHex(StringBuffer sb, byte b) {
		sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String enString=encrypt("1234567891011","password");//加密后的
		Thread.sleep(2000);
		String deString2=decrypt("1234567891011",enString);
		
		System.out.println("原始=>"+"password");
		System.out.println("加密后=>"+enString);
		System.out.println("解密后=>"+deString2);
	}
	
	
	//加密
	private static String encrypt(String key,String passwordString) throws Exception
	{
		String des=encryptDES(passwordString);
		char[] keys=key.toCharArray();
		char[] deStrings=des.toCharArray();
		System.out.println(des);
		char[] temp=new char[deStrings.length];
		for (int i = 0; i < deStrings.length; i++) {
			if(i<keys.length)
			{
				temp[i]=(char) (keys[i]+deStrings[i]);
			}else
			{
				temp[i]=deStrings[i];
			}
			
		}
		return new String(temp);
	}
	
	//解密
	private static String decrypt(String key,String desString) throws Exception
	{
		char[] keys=key.toCharArray();
		char[] deStrings=desString.toCharArray();
		char[] temps=new char[deStrings.length];
		for (int i = 0; i < deStrings.length; i++) {
			if(i<keys.length)
			{
				temps[i]=(char) (deStrings[i]-keys[i]);
			}else
			{
				temps[i]=deStrings[i];
			}
		}
		String des=new String(temps);
		return decryptDES(des);
	}
	

}
